﻿using BSChuangXu.API.Read.Appicant.Commend.UserCommand;
using BSChuangXu.API.Read.DTO;
using BSChuangXu.Domain.User;
using BSChuangXu.ErrorCode;
using BSChuangXu.Infrastructure.Interface;
using MediatR;
using System.Data;

namespace BSChuangXu.API.Read.Appicant.CommendHandel.UserCommandHandler
{
    /// <summary>
    /// 查询用户列表
    /// </summary>
    public class GetUserCommandHandler : IRequestHandler<GetUserCommand, APIResult<List<UserDto>>>
    {
        private readonly IBaseRepository<UserModel> userRepos;
        private readonly IBaseRepository<RoleModel> roleRepos;
        private readonly IBaseRepository<UserRole> userRoleRepos;

        public GetUserCommandHandler(
            IBaseRepository<UserModel> userRepos,
            IBaseRepository<RoleModel> roleRepos,
            IBaseRepository<UserRole> userRoleRepos

            )
        {
            this.userRepos = userRepos;
            this.roleRepos = roleRepos;
            this.userRoleRepos = userRoleRepos;
        }
        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<UserDto>>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<UserDto>> result = new APIResult<List<UserDto>>();
            result.Code = APIEnum.操作成功;
            result.Msg = APIEnum.操作成功.ToString();
            var list = userRepos.GetAll().Result
           .Join(userRoleRepos.GetAll().Result, a => a.UserId, b => b.UserId, (a, b) => new { a, b })
           .Join(roleRepos.GetAll().Result, temp => temp.b.RoleId, o => o.RoleId, (temp, o) => new UserDto
           {
               UserId = temp.a.UserId,
               UserName = temp.a.UserName,
               DepId = temp.a.DepId,
               NickName = temp.a.NickName,
               Sex = temp.a.Sex,
               ProvinceId = temp.a.ProvinceId,
               CityId = temp.a.CityId,
               CountyId = temp.a.CountyId,
               HamletName = temp.a.HamletName,
               RoleId = o.RoleId,
               RoleName = o.RoleName,
               UserRoleId = temp.b.UserRoleId,
               CreateName = temp.a.CreateName,
               CreateTime = temp.a.CreateTime,
               UpdateName = temp.a.UpdateName,
               UpdateTime = temp.a.UpdateTime,
               IsDel = temp.a.IsDel,
           }).GroupBy(x => x.UserId) // 按用户编号分组
           .Select(group => new UserDto
           {
               UserId = group.Key,
               UserName = group.First().UserName,
               DepId = group.First().DepId,
               NickName = group.First().NickName,
               Sex = group.First().Sex,
               ProvinceId = group.First().ProvinceId,
               CityId = group.First().CityId,
               CountyId = group.First().CountyId,
               HamletName = group.First().HamletName,
               CreateName = group.First().CreateName,
               CreateTime = group.First().CreateTime,
               UpdateName = group.First().UpdateName,
               UpdateTime = group.First().UpdateTime,
               RoleId = group.First().RoleId,
               RoleName = string.Join(", ", group.Select(x => x.RoleName)), // 合并角色名称
               UserRoleId = group.First().UserRoleId,
               IsDel = group.First().IsDel
           }).Where(x => x.IsDel == false);
            result.data = list.ToList();
            return await Task.FromResult(result);
        }
    }
}
